/**
 * Project Name:fire-industry-DM-center
 * File Name:ManagerServer.java
 * Package Name:com.firestone.web.center
 * Date:2018年3月29日下午6:45:09
 *
*/

package com.firestone.web.center;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.manager.ManagerDTO;
import com.firestone.bean.dto.user.User;
import com.firestone.bean.entity.mysql.primary.ImCenterManager;
import com.firestone.bean.po.manager.SavePO;
import com.firestone.bean.po.manager.UpdatePO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.manager.ManagerVO;
import com.firestone.converters.center.ConverterManager;
import com.firestone.core.constants.ConstantKey;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.service.center.IImCenterManagerService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: TODO(管理员控制器)
 * @author CAIYJ
 * @date 2018年3月29日 下午6:45:09
 */
@Api(value = "管理员控制器", tags = { "管理员接口文档" })
@RestController
@RequestMapping(value = "manager")
public class ManagerServer extends BaseServer {

    @Autowired
    private IImCenterManagerService imCenterManagerService;

    /**
     * @Description: TODO(列表)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:58:01
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "列表", notes = "列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "管理员名称", required = false, dataType = "String", paramType = "query") })
    @RequestMapping(value = "/list")
    public ReturnDTO<BaseListVO<ManagerVO>> list(String name, PageDTO pageDTO)
            throws Exception {
        Page<ManagerDTO> page = new Page<>(pageDTO.getPage(),
                pageDTO.getSize());
        page = imCenterManagerService.listManager(page, name);
        return ReturnDTO.success(ConverterManager.convertListVO(page));
    }

    /**
     * @Description: TODO(新增管理员)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:58:08
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "save")
    public ReturnDTO<String> save(SavePO savePO) throws Exception {
        ImCenterManager imCenterManager = new ImCenterManager();
        BeanUtils.copyProperties(savePO, imCenterManager);
        User user = getUserInfo();
        imCenterManager.setPassword(DigestUtils
                .md5DigestAsHex(imCenterManager.getPassword().getBytes()));
        imCenterManager.setCreator(user.getId());
        if (imCenterManagerService.saveManager(imCenterManager,
                savePO.getRoleId())) {
            return ReturnDTO.success("添加成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }

    /**
     * @Description: TODO(修改管理员)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:58:28
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/update/{id}")
    public ReturnDTO<String> update(@PathVariable Long id, UpdatePO updatePO)
            throws Exception {
        ImCenterManager imCenterManager = new ImCenterManager();
        BeanUtils.copyProperties(updatePO, imCenterManager);
        User user = getUserInfo();
        imCenterManager.setId(id);
        imCenterManager.setReviser(user.getId());
        if (imCenterManagerService.updateManager(imCenterManager,
                updatePO.getRoleId())) {
            return ReturnDTO.success("修改成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_update_failed);
    }

    /**
     * @Description: TODO(删除管理员)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:58:36
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/remove/{id}")
    public ReturnDTO<String> remove(@PathVariable Long id) throws Exception {
        ImCenterManager imCenterManager = new ImCenterManager();
        imCenterManager.setId(id);
        imCenterManager.setIsDelete(ConstantKey.DELETE_TRUE);
        if (imCenterManagerService.removeManager(imCenterManager)) {
            return ReturnDTO.success("删除失败");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_delete_failed);
    }
}
